using System.Collections.Generic;
using Photon;
using UnityEngine;

public class EquipmentManager : Photon.MonoBehaviour
{
	public enum EquipmentItemType
	{
		Armor = 0,
		Hat = 1,
		Boot = 2,
		Utils = 3
	}

	[SerializeField]
	private Transform armorTors;

	[SerializeField]
	private Transform armorArmLeftTop;

	[SerializeField]
	private Transform armorArmRightTop;

	[SerializeField]
	private Transform armorArmLeftDown;

	[SerializeField]
	private Transform armorArmRightDown;

	[SerializeField]
	private Transform armorLegLeftTop;

	[SerializeField]
	private Transform armorLegRightTop;

	[SerializeField]
	private Transform armorLegLeftDown;

	[SerializeField]
	private Transform armorLegRightDown;

	[SerializeField]
	private Transform hatPoint;

	[SerializeField]
	private Transform leftBootPoint;

	[SerializeField]
	private Transform rightBootPoint;

	[SerializeField]
	private PlayerBehavior player;

	private ArmorItem equipedArmor;

	private EquipmentItem equipedHat;

	private EquipmentItem equipedBootR;

	private EquipmentItem equipedBootL;

	private EquipmentItem equipedUtil;

	private bool _isVisible = true;

	private bool visibleEquip = true;

	public Transform ArmorTors
	{
		get
		{
			return armorTors;
		}
	}

	public Transform ArmorArmLeftTop
	{
		get
		{
			return armorArmLeftTop;
		}
	}

	public Transform ArmorArmRightTop
	{
		get
		{
			return armorArmRightTop;
		}
	}

	public Transform ArmorArmLeftDown
	{
		get
		{
			return armorArmLeftDown;
		}
	}

	public Transform ArmorArmRightDown
	{
		get
		{
			return armorArmRightDown;
		}
	}

	public Transform ArmorLegLeftTop
	{
		get
		{
			return armorLegLeftTop;
		}
	}

	public Transform ArmorLegRightTop
	{
		get
		{
			return armorLegRightTop;
		}
	}

	public Transform ArmorLegLeftDown
	{
		get
		{
			return armorLegLeftDown;
		}
	}

	public Transform ArmorLegRightDown
	{
		get
		{
			return armorLegRightDown;
		}
	}

	public ArmorItem EquipedArmor
	{
		get
		{
			return equipedArmor;
		}
	}

	public bool VisibleArmor
	{
		get
		{
			return _isVisible;
		}
		set
		{
			_isVisible = value;
			if (equipedArmor != null)
			{
				SetArmorVisible(value);
			}
		}
	}

	public static string GetEquipedArmorId
	{
		get
		{
			return Load.LoadString("body");
		}
	}

	public static string GetEquipedHatId
	{
		get
		{
			return Load.LoadString("hats");
		}
	}

	public static string GetEquipedBootsId
	{
		get
		{
			return Load.LoadString("boots");
		}
	}

	public void EquipArmor(string key)
	{
		if (player.maxArmor != 0)
		{
			player.currentArmorPercentages = (float)player.armor / (float)player.maxArmor * 100f;
		}
		else
		{
			player.currentArmorPercentages = 100f;
		}
		RefreshItemsBonus();
		if (equipedArmor != null)
		{
			SetArmorVisible(false);
		}
		if (!settings.offlineMode && base.photonView.isMine)
		{
			base.photonView.RPC("EquipOnline", PhotonTargets.OthersBuffered, 0, key);
		}
		if ((settings.offlineMode || base.photonView.isMine) && equipedArmor != null)
		{
			player.AddSpecialProperty(equipedArmor.GetSpecialProperties());
		}
	}

	public void PutOnArmor(ArmorItem needArmor)
	{
		if (equipedArmor != null)
		{
			Object.Destroy(equipedArmor.gameObject);
			equipedArmor = null;
		}
		if (needArmor != null)
		{
			GameObject gameObject = Object.Instantiate(needArmor.gameObject);
			gameObject.name = needArmor.gameObject.name;
			equipedArmor = gameObject.GetComponent<ArmorItem>();
			gameObject.transform.parent = base.transform;
			equipedArmor.EquipOn(this);
			equipedArmor.SetLayer(base.gameObject.layer);
			equipedArmor.SetLocalScale(Vector3.one);
		}
	}

	public void EquipUtils(string key)
	{
		RefreshItemsBonus();
		if ((settings.offlineMode || base.photonView.isMine) && equipedUtil != null)
		{
			player.AddSpecialProperty(equipedUtil.GetSpecialProperties());
		}
		if (!settings.offlineMode && base.photonView.isMine)
		{
			base.photonView.RPC("EquipOnline", PhotonTargets.OthersBuffered, 3, key);
		}
	}

	public void EquipHat(string key)
	{
		RefreshItemsBonus();
		if ((settings.offlineMode || base.photonView.isMine) && equipedHat != null)
		{
			player.AddSpecialProperty(equipedHat.GetSpecialProperties());
		}
		if (!settings.offlineMode && base.photonView.isMine)
		{
			base.photonView.RPC("EquipOnline", PhotonTargets.OthersBuffered, 1, key);
		}
	}

	public void UnEquipItem(EquipmentItem item)
	{
		if ((settings.offlineMode || base.photonView.isMine) && item != null)
		{
			player.RemoveSpecialProperty(item);
		}
	}

	public void PutOnUtils(EquipmentItem util)
	{
		if (equipedUtil != null)
		{
			Object.Destroy(equipedUtil.gameObject);
			equipedUtil = null;
		}
		if (util != null)
		{
			GameObject gameObject = Object.Instantiate(util.gameObject);
			gameObject.name = util.gameObject.name;
			equipedUtil = gameObject.GetComponent<EquipmentItem>();
			gameObject.transform.parent = base.transform;
			PutOnPartItem(gameObject, hatPoint);
			if (!visibleEquip)
			{
				equipedUtil.gameObject.SetActive(false);
			}
		}
		if (Device.indexWeakDevice != 0 && player != null && !player.isMine && !settings.offlineMode)
		{
			List<Renderer> list = new List<Renderer>();
			if (equipedUtil != null)
			{
				list.AddRange(equipedUtil.GetComponentsInChildren<Renderer>());
			}
			list.ForEach(delegate(Renderer r)
			{
				r.enabled = false;
			});
		}
	}

	public void PutOnHat(EquipmentItem hat)
	{
		if (equipedHat != null)
		{
			Object.Destroy(equipedHat.gameObject);
			equipedHat = null;
		}
		if (hat != null)
		{
			GameObject gameObject = Object.Instantiate(hat.gameObject);
			gameObject.name = hat.gameObject.name;
			equipedHat = gameObject.GetComponent<EquipmentItem>();
			gameObject.transform.parent = base.transform;
			PutOnPartItem(gameObject, hatPoint);
			if (!visibleEquip)
			{
				equipedHat.gameObject.SetActive(false);
			}
		}
		if (Device.indexWeakDevice != 0 && player != null && !player.isMine && !settings.offlineMode)
		{
			List<Renderer> list = new List<Renderer>();
			if (equipedHat != null)
			{
				list.AddRange(equipedHat.GetComponentsInChildren<Renderer>());
			}
			list.ForEach(delegate(Renderer r)
			{
				r.enabled = false;
			});
		}
	}

	public void EquipBoots(string key)
	{
		RefreshItemsBonus();
		if (!settings.offlineMode && base.photonView.isMine)
		{
			base.photonView.RPC("EquipOnline", PhotonTargets.OthersBuffered, 2, key);
		}
		if ((settings.offlineMode || base.photonView.isMine) && equipedBootR != null)
		{
			player.AddSpecialProperty(equipedBootR.GetSpecialProperties());
		}
	}

	public void PutOnBoots(EquipmentItem bootR, EquipmentItem bootL)
	{
		if (equipedBootR != null)
		{
			Object.Destroy(equipedBootR.gameObject);
			equipedBootR = null;
		}
		if (equipedBootL != null)
		{
			Object.Destroy(equipedBootL.gameObject);
			equipedBootL = null;
		}
		if (bootL != null)
		{
			GameObject gameObject = Object.Instantiate(bootL.gameObject);
			gameObject.name = bootL.gameObject.name;
			equipedBootL = gameObject.GetComponent<EquipmentItem>();
			gameObject.transform.parent = base.transform;
			PutOnPartItem(gameObject, leftBootPoint);
			if (!visibleEquip)
			{
				equipedBootL.gameObject.SetActive(false);
			}
		}
		if (bootR != null)
		{
			GameObject gameObject2 = Object.Instantiate(bootR.gameObject);
			gameObject2.name = bootR.gameObject.name;
			equipedBootR = gameObject2.GetComponent<EquipmentItem>();
			gameObject2.transform.parent = base.transform;
			PutOnPartItem(gameObject2, rightBootPoint);
			if (!visibleEquip)
			{
				equipedBootR.gameObject.SetActive(false);
			}
		}
		if (Device.indexWeakDevice != 0 && player != null && !player.isMine && !settings.offlineMode)
		{
			List<Renderer> list = new List<Renderer>();
			if (equipedBootR != null)
			{
				list.AddRange(equipedBootR.GetComponentsInChildren<Renderer>());
			}
			if (equipedBootL != null)
			{
				list.AddRange(equipedBootL.GetComponentsInChildren<Renderer>());
			}
			list.ForEach(delegate(Renderer r)
			{
				r.enabled = false;
			});
		}
	}

	private void SetArmorVisible(bool value)
	{
		if (equipedArmor.torsTop != null)
		{
			equipedArmor.torsTop.SetActive(_isVisible);
		}
		if (equipedArmor.armLeftTop != null)
		{
			equipedArmor.armLeftTop.SetActive(_isVisible);
		}
		if (equipedArmor.armRightTop != null)
		{
			equipedArmor.armRightTop.SetActive(_isVisible);
		}
		if (equipedArmor.armLeftDown != null)
		{
			equipedArmor.armLeftDown.SetActive(_isVisible);
		}
		if (equipedArmor.armRightDown != null)
		{
			equipedArmor.armRightDown.SetActive(_isVisible);
		}
		if (equipedArmor.legLeftTop != null)
		{
			equipedArmor.legLeftTop.SetActive(_isVisible);
		}
		if (equipedArmor.legRightTop != null)
		{
			equipedArmor.legRightTop.SetActive(_isVisible);
		}
		if (equipedArmor.legLeftDown != null)
		{
			equipedArmor.legLeftDown.SetActive(_isVisible);
		}
		if (equipedArmor.legRightDown != null)
		{
			equipedArmor.legRightDown.SetActive(_isVisible);
		}
	}

	private void RefreshItemsBonus()
	{
		RefreshArmor();
		RefreshDamageMultiplier();
		RefreshIgnors();
	}

	private void RefreshIgnors()
	{
		player.SetImmun(EffectType.Non);
		if (equipedArmor != null)
		{
			player.SetImmun(equipedArmor.ignoreEffects);
		}
		if (equipedBootR != null)
		{
			player.SetImmun(equipedBootR.ignoreEffects);
		}
		if (equipedBootL != null)
		{
			player.SetImmun(equipedBootL.ignoreEffects);
		}
		if (equipedHat != null)
		{
			player.SetImmun(equipedHat.ignoreEffects);
		}
		if (equipedUtil != null)
		{
			player.SetImmun(equipedUtil.ignoreEffects);
		}
	}

	private void RefreshArmor()
	{
		int num = 0;
		if (equipedArmor != null)
		{
			num += equipedArmor.protection;
		}
		if (equipedBootR != null)
		{
			num += equipedBootR.protection;
		}
		if (equipedBootL != null)
		{
			num += equipedBootL.protection;
		}
		if (equipedHat != null)
		{
			num += equipedHat.protection;
		}
		if (equipedUtil != null)
		{
			num += equipedUtil.protection;
		}
		player.maxArmor = num;
		player.armor = Mathf.RoundToInt((float)player.maxArmor * player.currentArmorPercentages / 100f);
	}

	private void RefreshDamageMultiplier()
	{
		player.ClearDamageMultiplier();
		if (equipedArmor != null)
		{
			equipedArmor.damageMultiplier.ForEach(delegate(DamageTypeMultiplier m)
			{
				player.AddDamageMultiplier(m);
			});
		}
		if (equipedBootR != null)
		{
			equipedBootR.damageMultiplier.ForEach(delegate(DamageTypeMultiplier m)
			{
				player.AddDamageMultiplier(m);
			});
		}
		if (equipedBootL != null)
		{
			equipedBootL.damageMultiplier.ForEach(delegate(DamageTypeMultiplier m)
			{
				player.AddDamageMultiplier(m);
			});
		}
		if (equipedHat != null)
		{
			equipedHat.damageMultiplier.ForEach(delegate(DamageTypeMultiplier m)
			{
				player.AddDamageMultiplier(m);
			});
		}
		if (equipedUtil != null)
		{
			equipedUtil.damageMultiplier.ForEach(delegate(DamageTypeMultiplier m)
			{
				player.AddDamageMultiplier(m);
			});
		}
	}

	[PunRPC]
	public void EquipOnline(int type, string key)
	{
		if (!base.photonView.isMine)
		{
			switch ((EquipmentItemType)type)
			{
			case EquipmentItemType.Armor:
				LoadAndEquipArmor(key, this);
				break;
			case EquipmentItemType.Hat:
				LoadAndEquipHat(key, this);
				break;
			case EquipmentItemType.Boot:
				LoadAndEquipBoots(key, this);
				break;
			case EquipmentItemType.Utils:
				LoadAndEquipUtils(key, this);
				break;
			}
		}
	}

	public void EquipFromProps(int type, string key)
	{
		if (!base.photonView.isMine)
		{
			switch ((EquipmentItemType)type)
			{
			case EquipmentItemType.Armor:
				LoadAndEquipArmor(key, this);
				break;
			case EquipmentItemType.Hat:
				LoadAndEquipHat(key, this);
				break;
			case EquipmentItemType.Boot:
				LoadAndEquipBoots(key, this);
				break;
			case EquipmentItemType.Utils:
				LoadAndEquipUtils(key, this);
				break;
			}
		}
	}

	public static void LoadAndEquipArmor(string key, EquipmentManager forArmMan, bool needEquip = true)
	{
		ArmorItem needArmor = null;
		if (forArmMan.player != null && forArmMan.photonView.isMine)
		{
			forArmMan.player.props.armorName = key;
			forArmMan.player.photonPlayer.UpdateProperty(PlayerPropsKeys.armor, key);
		}
		if (!key.Equals(string.Empty))
		{
			needArmor = Resources.Load<ArmorItem>("Armors/" + key);
		}
		if (needEquip)
		{
			forArmMan.UnEquipItem(forArmMan.equipedArmor);
		}
		forArmMan.PutOnArmor(needArmor);
		if (needEquip)
		{
			forArmMan.EquipArmor(key);
		}
	}

	public static void LoadAndEquipUtils(string key, EquipmentManager forArmMan, bool needEquip = true)
	{
		EquipmentItem util = null;
		if (forArmMan.player != null && forArmMan.photonView.isMine)
		{
			forArmMan.player.props.utilsName = key;
			forArmMan.player.photonPlayer.UpdateProperty(PlayerPropsKeys.utils, key);
		}
		if (!key.Equals(string.Empty))
		{
			util = Resources.Load<EquipmentItem>("Utils/" + key);
		}
		if (needEquip)
		{
			forArmMan.UnEquipItem(forArmMan.equipedUtil);
		}
		forArmMan.PutOnUtils(util);
		if (needEquip)
		{
			forArmMan.EquipUtils(key);
		}
	}

	public static void LoadAndEquipHat(string key, EquipmentManager forArmMan, bool needEquip = true)
	{
		EquipmentItem hat = null;
		if (forArmMan.player != null && forArmMan.photonView.isMine)
		{
			forArmMan.player.props.hatName = key;
			forArmMan.player.photonPlayer.UpdateProperty(PlayerPropsKeys.hat, key);
		}
		if (!key.Equals(string.Empty))
		{
			hat = Resources.Load<EquipmentItem>("Hats/" + key);
		}
		if (needEquip)
		{
			forArmMan.UnEquipItem(forArmMan.equipedHat);
		}
		forArmMan.PutOnHat(hat);
		if (needEquip)
		{
			forArmMan.EquipHat(key);
		}
	}

	public static void LoadAndEquipBoots(string key, EquipmentManager forArmMan, bool needEquip = true)
	{
		EquipmentItem bootR = null;
		EquipmentItem bootL = null;
		if (forArmMan.player != null && forArmMan.photonView.isMine)
		{
			forArmMan.player.props.bootsName = key;
			forArmMan.player.photonPlayer.UpdateProperty(PlayerPropsKeys.boots, key);
		}
		if (!key.Equals(string.Empty))
		{
			bootR = Resources.Load<EquipmentItem>("Boots/" + key + "R");
			bootL = Resources.Load<EquipmentItem>("Boots/" + key + "L");
		}
		if (needEquip)
		{
			forArmMan.UnEquipItem(forArmMan.equipedBootR);
		}
		forArmMan.PutOnBoots(bootR, bootL);
		if (needEquip)
		{
			forArmMan.EquipBoots(key);
		}
	}

	public void EquipCurrentArmor()
	{
		LoadAndEquipArmor(GetEquipedArmorId, this);
	}

	public void EquipCurrentHat()
	{
		LoadAndEquipHat(GetEquipedHatId, this);
	}

	public void EquipCurrentBoots()
	{
		LoadAndEquipBoots(GetEquipedBootsId, this);
	}

	private void PutOnCurrentArmor()
	{
		LoadAndEquipArmor(GetEquipedArmorId, this, false);
	}

	private void PutOnCurrentHat()
	{
		LoadAndEquipHat(GetEquipedHatId, this, false);
	}

	private void PutOnCurrentBoots()
	{
		LoadAndEquipBoots(GetEquipedBootsId, this, false);
	}

	private void PutOnPartItem(GameObject part, Transform pointAnchor)
	{
		if (part != null)
		{
			part.transform.parent = pointAnchor;
			part.transform.localPosition = Vector3.zero;
			part.transform.localRotation = Quaternion.identity;
			part.transform.localScale = Vector3.one;
			Transform[] componentsInChildren = part.GetComponentsInChildren<Transform>(true);
			foreach (Transform transform in componentsInChildren)
			{
				transform.gameObject.layer = base.gameObject.layer;
			}
		}
	}

	public void SetVisibleEquip(bool value)
	{
		visibleEquip = value;
		if (equipedHat != null)
		{
			equipedHat.gameObject.SetActive(value);
		}
		if (equipedBootR != null)
		{
			equipedBootR.gameObject.SetActive(value);
		}
		if (equipedBootL != null)
		{
			equipedBootL.gameObject.SetActive(value);
		}
	}

	public void EquipAllItems()
	{
		EquipCurrentArmor();
		EquipCurrentHat();
		EquipCurrentBoots();
	}

	public void PutOnAllItems()
	{
		PutOnCurrentArmor();
		PutOnCurrentHat();
		PutOnCurrentBoots();
	}
}
